import 'dart:async';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/http/api.dart';
import 'package:flutter_demo/other/application.dart';
import 'package:flutter_demo/widget/buttons.dart';
import 'package:flutter_demo/widget/tiny_widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';

Future<bool?> showChangePwdDialog(BuildContext context) {
  return showDialog<bool>(
    context: context,
    barrierDismissible: false,
    builder: (context) => ChangePwdDialog()
  );
}

class ChangePwdDialog extends StatelessWidget {

  String? _newPwd;
  GlobalKey _formKey = GlobalKey<FormState>();
  TextEditingController _cPasswordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(
        '修改密码',
        style: TextStyle(
          color: Colors.black87,
        ),
      ),
      content: Form(
        key: _formKey,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 10),
              child: TextFormField(
                obscureText: true,
                decoration: const InputDecoration(
                  hintText: '原密码',
                  border: InputBorder.none,
                ),
                validator: (value) {
                  if (value!.trim().isEmpty) {
                    return '原密码不能为空';
                  } else if (value.trim().length > 15) {
                    return '原密码超出最大长度';
                  } else {
                    return null;
                  }
                },
              ),
              decoration: BoxDecoration(
                border: Border.all(color: Colors.black26, width: 1),
                borderRadius: BorderRadius.circular(5),
              ),
            ),
            C(10),
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 10),
              child: TextFormField(
                obscureText: true,
                decoration: const InputDecoration(
                  hintText: '新密码',
                  border: InputBorder.none,
                ),
                validator: (value) {
                  if (value!.trim().isEmpty) {
                    return '新密码不能为空';
                  } else if (value.trim().length > 15) {
                    return '新密码超出最大长度';
                  } else {
                    _newPwd = value;
                    return null;
                  }
                },
              ),
              decoration: BoxDecoration(
                border: Border.all(color: Colors.black26, width: 1.0),
                borderRadius: BorderRadius.circular(5.0),
              ),
            ),
            C(10),
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 10),
              child: TextFormField(
                controller: _cPasswordController,
                obscureText: true,
                decoration: const InputDecoration(
                  hintText: '再输入一次新密码',
                  border: InputBorder.none,
                ),
                validator: (value) {
                  if (value!.trim().isEmpty) {
                    return '新密码不能为空';
                  } else if (value.trim().length > 15) {
                    return '新密码超出最大长度';
                  } else if (_newPwd! != value) {
                    return '两次输入不相同';
                  } else {
                    return null;
                  }
                },
              ),
              decoration: BoxDecoration(
                border: Border.all(color: Colors.black26, width: 1.0),
                borderRadius: BorderRadius.circular(5.0),
              ),
            ),
          ],
        ),
      ),
      actions: <Widget>[
        BaseButton(
          radius: 5,
          buttonColor: Colors.white70,
          withBorder: true,
          withShadow: false,
          width: 64,
          height: 28,
          centerButton: false,
          onTap: () => Navigator.of(context).pop(),
          child: const Text(
            '取消',
            style: TextStyle(
              color: Colors.black87,
            ),
          ),
        ),
        BaseButton(
          radius: 5,
          withShadow: false,
          width: 64,
          height: 28,
          centerButton: false,
          onTap: () {
            if ((_formKey.currentState as FormState).validate())
              validatePassed(context);
          },
          child: const Text(
            '确定',
            style: TextStyle(
              color: Colors.white,
            ),
          ),
        ),
      ],
    );
  }

  void validatePassed(BuildContext context) async {
    Response response;

    if (Application.currentType == AccountType.company)
      response = await SystemApi.c_repassword(
        _cPasswordController.text, Application.currentAccount);
    else
      response = await SystemApi.p_repassword(
          _cPasswordController.text, Application.currentAccount);

    if (response.statusCode == 200 && response.data['code'] == '1') {
      Fluttertoast.showToast(msg: response.data['msg']);
      Navigator.of(context).pop();
    } else if (response.data['code'] != '1') {
      Fluttertoast.showToast(msg: response.data['msg']);
    } else {
      Fluttertoast.showToast(msg: response.statusCode.toString());
    }
  }
}